// Driver.cpp

// Header Space
#include "MyAppointment.h"
#include "MyArray.h"
#include <iostream>
#include <fstream>
using namespace std;


// Main Body of the program
int main()
{
	// Creates the array with 31 spots for appointments
	MyArray<MyAppointment> appointList(31);

	// Contains user selection
	int select = 0;
	
	// Contains the new Date and Time variables to when editing an Appointment
	int newMonth = 0;
	int newDay = 0;
	int newYear = 0;
	int newHour = 0;
	int newMinute = 0;

	// A counter to keep track of how many appointments you have
	int appointCount = 0;

	// Holds user selection of which appointment when editing and deleting
	int select1 = 0;

	// Holder Variables when loading from a file
	long days = 0;
	long ticks = 0;

	// Holds start and end year for searching between years for appointments
	int startYear = 0;
	int endYear = 0;
	
	// Loops until the user inputs a valid selection
	do
	{
		// User interface
	cout << "================== Appointment Keeper =================\n";
	cout << "        Please enter the number of the selection       \n";
	cout << " 1 - Load Appointments from File                       \n";
	cout << " 2 - Create New Appointments File(Space for 31 Appointments\n";
	cout << " 3 - Exit Program                                      \n";
	cout << "=======================================================\n";

	cin >> select;
		
	// Case statement for the user selection from the above GUI
	switch(select)
	{
	case 1:
		{
			// Declares a ifstream
			ifstream file;

			// Opens the appropriate txt file
			file.open("AppointmentList.txt");

			// Reads the number at the top showing how 
			// many appointments are in the array
			file >> appointCount;
			
			// Loops through for each of the elements in the array
			for (int i = 0; i < appointCount; i++)
			{
				// looks at the character to see if it's a 
				// new line, if it is, removes it
			if (file.peek() == '\n')
				file.get();
			// outputs the First name of the appointment
			file >> appointList[i]._FirstName;

			if (file.peek() == '\n')
				file.get();
			// Outputs the Last name of the appointment
			file >> appointList[i]._LastName;

			if (file.peek() == '\n')
				file.get();
			
			// Outputs the appointment description
			file >> appointList[i]._Description;
			// Outputs the appointment date in term of days since
			// January 1, year 1
			file >> days;
			// Calls the SetDate function of the appointment object
			appointList[i]._Date.SetDate(days);
			// Outputs the appointment time in terms of ticks since
			// midnight
			file >> ticks;
			// calls the SetTicks function of the appointment object
			appointList[i]._Time.SetTicks(ticks);
			}
			//closes the file
			file.close();
			break;
		}
	case 2:
		{
			// Array is already made, so nothing needs to be done.
			break;
		}
	case 3:
		{
			cout << "Good Bye";
			return 0;
		}

	default:
		{
			cout << "Please Enter a valid selection\n";
		}
	}
	
	// if the user enters a valid selection, breaks out of the loop
	if (select == 2 || select == 1)
	{
		select = 0;
		break;
	}

	}
	while(select != 3);


	do
	{
	cout << "================== Appointment Keeper =================\n";
	cout << "        Please enter the number of the selection       \n";
	cout << " 1 - Add New Appointment                               \n";
	cout << " 2 - Edit Existing Appointment                         \n";
	cout << " 3 - Delete an Appointment								\n";
	cout << " 4 - Save Appointment List                             \n";
	cout << " 5 - Show Appointments between two dates               \n";
	cout << " 6 - Exit Program                                      \n";
	cout << "=======================================================\n";

	cin >> select;

	switch(select)
	{
	case 1:
		{
			// Increments the Array size to allow for more input
			appointList.IncrementSize();			
			// Sets the variables of the object to allow ease of access
			cout << "Enter the first name of the person the appointment is with: \n";
			cin >> appointList[appointCount]._FirstName;
			cout << "Enter the last name of the person the appoinment is with: \n";
			cin >> appointList[appointCount]._LastName;
			cout << "Enter the description of what the appointment is for: \n";
			cin >> appointList[appointCount]._Description;

			// Captures the user's input and uses it to set the date and time
			cout << "Enter the Month of the appointment: \n";
			cin >> newMonth;
			appointList[appointCount]._Date.SetMonth(newMonth);
			cout << "Enter the Day of the appointment: \n";
			cin >> newDay;
			appointList[appointCount]._Date.SetDay(newDay);
			cout << "Enter the Year of the appointment: \n";
			cin >> newYear;
			appointList[appointCount]._Date.SetYear(newYear);
			cout << "Enter the Hour of the appointment: \n";
			cin >> newHour;
			appointList[appointCount]._Time.SetHours(newHour);
			cout << "Enter the Minute of the appointment: \n";
			cin >> newMinute;
			appointList[appointCount]._Time.SetMinutes(newMinute);

			// Resets all the variables to 0 to prevent the 
			// user from putting in accidental input
			select = 0;
			newMonth = 0;
			newDay = 0;
			newYear = 0;
			newHour = 0;
			newMinute = 0;
			appointCount++;
			break;

		}

	case 2:
		{
			// Resets the select to 0 to prevent accidental input
			select = 0;
			cout << "Choose an Appointment to Edit: \n";
			// loops through and outputs all the appointments
			for(int i = 0; i < appointCount; i++)
			{
				cout << i << " - " << appointList.At(i) << "\n";
			}
			// Ask for which appointment the user wants to edit
			cin >> select1;

			cout << "================== Appointment Keeper =================\n";
			cout << "        Please enter the number of the selection       \n";
			cout << " 1 - Edit Name of Appointment                    \n";
			cout << " 2 - Edit Description of an Appointment				\n";
			cout << " 3 - Edit Date of Appointment                          \n";
			cout << " 4 - Edit Time of Appointment                          \n";
			cout << " 5 - Exit Program                         \n";
			cout << "=======================================================\n";

			cin >> select;
			
			// Use user entered value to specify the array position to modify 
			switch(select)
			{
			case 1:
				{
					cout << "Enter the first name of the person the appointment is with: \n";
					cin >> appointList[select1]._FirstName;
					cout << "Enter the last name of the person the appoinment is with: \n";
					cin >> appointList[select1]._LastName;
					break;
				}
			case 2:
				{
					cout << "Enter the description of wha the appointment is for: \n";
					cin >> appointList[select1]._Description;
					break;
				}
			case 3:
				{
					cout << "Enter the Month of the appointment: \n";
					cin >> newMonth;
					appointList[appointCount]._Date.SetMonth(newMonth);
					cout << "Enter the Day of the appointment: \n";
					cin >> newDay;
					appointList[select1]._Date.SetDay(newDay);
					cout << "Enter the Year of the appointment: \n";
					cin >> newYear;
					appointList[select1]._Date.SetYear(newYear);
					
					break;
				}
			case 4:
				{
					cout << "Enter the Hour of the appointment: \n";
					cin >> newHour;
					appointList[select1]._Time.SetHours(newHour);
					cout << "Enter the Minute of the appointment: \n";
					cin >> newMinute;
					appointList[select1]._Time.SetMinutes(newMinute);
					break;
				}
			case 5:
				{
					return 0;
				}
			}
	case 3:
		{
			// reset selection to prevent accidental input
			select = 0;
			cout << "Choose an Appointment to delete: \n";
			for(int i = 0; i < appointCount; i++)
			{
				cout << i << " - " << appointList.At(i) << "\n";
			}
			cin >> select;
			
			// Calls the RemoveAt function found in the MyArray class
			appointList.RemoveAt(select);
			break;
		}
	case 4:
		{
			// Creates the Output stream object
			ofstream file;

			// Opens the file to put things into
			file.open("AppointmentList.txt");
				
			// Throws the Appointment Count into the file
			file << appointCount << "\n";
			
			// loops through the list, and puts the elements of the array into the file
			for (int i = 0; i < appointCount; i++)
			{	
				file << appointList[i]._FirstName << "\n";
				file << appointList[i]._LastName << "\n";
				file << appointList[i]._Description << "\n";
				file << appointList[i]._Date.GetDateSerial() << "\n";
				file << appointList[i]._Time.GetTicks() << "\n";
			}
			// closes the file
			file.close();
			break;

		}
	case 5:
		{
			// Asks user where to start the search
			cout << "Enter the Year you want to start the search from: ";
			cin >> startYear;
			// Asks the user where to end the search
			cout << "Enter the Year you want to search to: ";
			cin >> endYear;
			
			// loops through the array 
			for (int i = 0; i < appointCount; i++)
			{
				// Checks if the year is withen the search boundries.
				if (appointList[i]._Date.GetYear() >= startYear && appointList[i]._Date.GetYear() <= endYear)
					cout << appointList.At(i);
			}
			break;			
		}
	case 6:
		{
			return 0;
		}
		}
	}
	/*if (select == 1 || select == 2 || select == 3 || select == 4)
		break;*/
	}while(select != 6);
		

	return 0;
}